//
//  foutThTableViewController.m
//  mintHome
//
//  Created by 朱彦名 on 16/6/23.
//  Copyright © 2016年 朱彦名. All rights reserved.
//

#import "foutThTableViewController.h"
#import "cellOfFirst.h"
#import "APPTools.h"
#import "modelOfMintHome.h"
#import "ViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "viewOfTag.h"
#import "tapGestureViewController.h"

@interface foutThTableViewController ()

@property (nonatomic, retain) NSMutableArray *arrOf4th;
@property (nonatomic, retain) NSMutableArray *arrOfData4;
@property (nonatomic, retain) NSMutableArray *arr1;
@end

@implementation foutThTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self handleData];
    
    [self.tableView registerClass:[cellOfFirst class] forCellReuseIdentifier:@"pool"];
    
    [self refresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _arrOf4th.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    cellOfFirst *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        
        cell.backgroundColor = [UIColor silverColor];
        
    }
    else{
        
        cell.backgroundColor = [UIColor whiteColor];
        
    }
    modelOfMintHome *model1 = _arrOf4th[indexPath.row];
    cell.labelOfTitle.text = model1.title;
    [cell.imageOfMint sd_setImageWithURL:[NSURL URLWithString:model1.cover_image_url]];
    cell.viewOfTag.labelOfTag.text = [NSString stringWithFormat:@"%ld",model1.likes_count];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 200;
}
- (void)handleData{
    self.arrOf4th = [NSMutableArray array];
    
    [APPTools GETWithURL:@"http://api.bohejiaju.com/v1/channels/12/items?limit=20&offset=0" par:nil success:^(id responseObject) {
        
        self.arrOfData4 = responseObject [@"data"][@"items"];
        
        self.arrOf4th = [modelOfMintHome modelHandlerWithArray:self.arrOfData4];
        
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
    } filed:^(NSError *error) {
        
    }];
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    tapGestureViewController * tapView = [[tapGestureViewController alloc]init];
    
    self.arr1 = [NSMutableArray array];
    
    
    for (NSDictionary *dic in self.arrOfData4) {
        [self.arr1 addObject:[dic objectForKey:@"url"]];
    }
    tapView.url4 = self.arr1[indexPath.row];
    
    [self.navigationController pushViewController:tapView animated:YES];
}
- (void)setLightDay{
    
    [NightManager setBackgroundColorWithView:self.view];
    [self.tableView reloadData];
    
}
- (void)setNightDay{
    
    [NightManager setBackgroundColorWithView:self.view];
    [self.tableView reloadData];
    
}
- (void)refresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleData)];
    [self.tableView.mj_header beginRefreshing];
}

@end
